package com.itheima.tlias84.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.utils.JwtUtil;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Resource
    EmpMapper empMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("开始执行http请求过滤");
//        String requestURI = request.getRequestURI();
//        if (requestURI.contains("login")) {
//            return true;
//        }
        String token = request.getHeader("token");

        //判断是否有token ->是否绕过登录
        if (Objects.isNull(token)) {
            response.setContentType("application/json; charset = UTF-8");
            response.setCharacterEncoding("UTF-8");
            //设置响应状态码
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            PrintWriter writer = response.getWriter();

            ResultUtil fail = ResultUtil.fail("请先登录");
            writer.write(JSONObject.toJSONString(fail));
            return false;
        }
        try {
            JSONObject jsonObject = JwtUtil.verifyToken(token);
            Emp emp = empMapper.selectEmpByUserName(jsonObject.getString("username"));
            if (Objects.isNull(emp) || !emp.getPassword().equals(jsonObject.getString("password"))){
                response.setContentType("application/json; charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                PrintWriter writer = response.getWriter();
                ResultUtil fail = ResultUtil.fail("无效的token");
                writer.write(JSONObject.toJSONString(fail));
                return false;
            }
        }catch (Exception e){
            response.setContentType("application/json; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(HttpStatus.SC_UNAUTHORIZED);
            PrintWriter writer = response.getWriter();
            ResultUtil fail = ResultUtil.fail("无效的token");
            writer.write(JSONObject.toJSONString(fail));
            return false;
        }
//        String dateParam = request.getParameter("date");
//        if (dateParam != null) {
//            // 修改日期/时间
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = sdf.parse(dateParam);
//            // 假设这里做了某种处理
//            // ...
//            // 将处理后的日期/时间重新放入请求参数
//            request.setAttribute("date", sdf.format(date));
//        }
        log.info("过滤完成");
        return true;
    }
}
